const fs = require("node:fs");
const tree = require('./tree.json');
const path = require('path');
(() => {
    const treeChildren = tree.children[0] || {}
    const pptArray = [{
        type: "cover",
        contents: { coverTitle: treeChildren.name, coverSubTitle: tree.subTitle, coverSpeaker: tree.speaker, coverDate: tree.speakerTime }
    }];

    pptArray[1] = {
        type: "catalog",
        contents:
        {
            calalogcontent: (treeChildren.children || []).map((c, i) => {
                const { name } = c;
                const id = i + 1;
                const index = id > 9 ? id : `0${id}`;
                return index + '.' + name + '\n'
            }).join('')
        }
    };
    const sessionAndContentArray = (treeChildren.children || []).map((c, i) => {
        const { name, children = [] } = c;
        const id = i + 1;
        const index = id > 9 ? id : `0${id}`;
        const sessionAndContent = [{
            type: "session",
            contents: { sessionnum: index, sessiontitle: name }
        }]
        sessionAndContent.push({
            type: "content",
            contents: {
                "contenttext": children.map(ct => {
                    const { name } = ct
                    return name + '\n'
                }).join('')
            }
        })

        return sessionAndContent
    })
    pptArray.push(...sessionAndContentArray.flat(Infinity))
    pptArray.push({ "type": "end", "contents": {} })
    fs.writeFileSync(path.resolve(__dirname, './ppt.json'), JSON.stringify(pptArray))

})()